<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="success" icon="el-icon-plus" style="margin-right: 20px"
        @click="handleCreate">
        新增
      </el-button>
      设备名称：<el-input v-model="listQuery.deviceName" placeholder="设备名称" style="width: 200px; margin-right: 20px"
        class="filter-item" @keyup.enter.native="handleFilter" />
      设备状态：<el-select v-model="listQuery.deviceStatus" placeholder="设备状态" clearable
        style="width: 200px; margin-right: 20px" class="filter-item">
        <el-option label="在线" value="1" />
        <el-option label="离线" value="0" />
      </el-select>

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px" type="info" icon="el-icon-refresh-right"
        @click="handleRefresh">
        重置
      </el-button>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column label="序号" prop="id" width="100">
        <template slot-scope="{ row, $index }">
          {{ (listQuery.pageNo - 1) * listQuery.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" prop="deviceName" />
      <el-table-column label="设备型号" prop="deviceModel" />
      <el-table-column label="设备SN" prop="deviceSn" />
      <el-table-column label="所属部门" prop="deptName" />
      <el-table-column label="设备负责人" prop="deviceManagerName" />
      <el-table-column label="设备状态" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.deviceStatus == 1" type="success" size="small">在线</el-tag>
          <el-tag v-else type="info" size="small">离线</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{ row, $index }">
          <el-button v-if="row.deviceStatus == 1" size="mini" type="success" @click="handleDetail(row)">
            查看
          </el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px"
        style="width: 500px; margin-left: 10px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="temp.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备型号" prop="deviceModel">
          <el-input v-model="temp.deviceModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="设备SN" prop="deviceSn">
          <el-input v-model="temp.deviceSn" placeholder="请输入设备SN" />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <el-select v-model="temp.deptId" class="filter-item" placeholder="请选择所属部门" @change="departmentChange">
            <el-option v-for="item in department" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备负责人" prop="deviceManagerName">
          <el-input v-model="temp.deviceManagerName" placeholder="请输入设备负责人" />
        </el-form-item>
        <el-form-item label="设备图片" prop="deviceImage">
          <el-upload action="" list-type="picture-card" :on-remove="handleRemove" :file-list="uploadList">
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消 </el-button>
        <el-button type="primary" :loading="btnLoading"
          @click="dialogStatus === 'create' ? createData() : updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  equipmentList,
  equipmentAdd,
  equipmentDel,
  equipmentDetail,
  equipmentUpdate,
  departmentList
} from '@/api/uavequipment'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Equipment',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      btnLoading: false,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        deviceName: undefined,
        deviceStatus: undefined
        // sort: "+id",
      },
      temp: {
        id: undefined,
        deviceName: '',
        deviceModel: '',
        deviceSn: '',
        deptId: '',
        deviceManagerName: '',
        deviceImage:''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        deviceName: [
          { required: true, message: '请输入设备名称', trigger: 'blur' }
        ],
        deviceModel: [
          { required: true, message: '请输入设备型号', trigger: 'blur' }
        ],
        deviceSn: [
          { required: true, message: '请输入设备SN', trigger: 'blur' }
        ],
        deptId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        deviceManagerName: [
          { required: true, message: '请输入设备负责人', trigger: 'change' }
        ],
      },
      uploadList: [],
      department: []
    }
  },
  created() {
    this.getList()
    this.getDepartment()
  },
  methods: {
    getList() {
      this.listLoading = true
      equipmentList(this.listQuery).then((response) => {
        this.list = response.list
        this.total = response.total
        this.listLoading = false
      })
    },
    getDepartment() {
      departmentList({
        pageNo: 1,
        pageSize: 200
      }).then((response) => {
        this.department = response.records
      })
    },
    handleRemove() { },
    departmentChange(e) {
      const info = this.department.filter((item) => {
        return item.deptId == e
      })
      this.temp.deviceManagerName = info[0].name
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleRefresh() {
      (this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        deviceName: undefined,
        deviceStatus: undefined
      }),
        this.getList()
    },
    handleDetail(row) {
      this.$router.push({ path: '/monitoring/detail' })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        deviceName: '',
        deviceModel: '',
        deviceSn: '',
        deptId: '',
        deviceManagerName: '',
        deviceImage:''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          equipmentAdd(this.temp).then(() => {
            this.btnLoading = false
            this.dialogFormVisible = false
            this.$notify({
              title: '提示',
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      console.log(this.temp)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          this.btnLoading = true
          equipmentUpdate(tempData).then(() => {
            this.btnLoading = false
            this.dialogFormVisible = false
            this.$notify({
              title: '提示',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除该设备？')
        .then(() => {
          equipmentDel(row.id).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
        .catch(() => { })
    }
  }
}
</script>
